var express = require('express');
var router = express.Router();
const { XiaoShouModel,PersonnelModel,PersonnelModels,mYprofileModels,mYprofileshuModels } = require('../model/hqh_model')
router.get('/region', async (req, res) => {
	res.send({})
})

// // 销售利润统计表
router.get('/getdata', async (req, res) => {
	let pagesize=req.query.pagesize
	if(!pagesize){
		pagesize=1
	}
	let datacount = await XiaoShouModel.find().count()
	let datapage = Math.ceil(await XiaoShouModel.find().count()/10)
	let data = await XiaoShouModel.find().limit(10).skip((pagesize-1)*10)
	res.send({ data,datacount,datapage })
})

// 人员档案 表   共享
router.get('/get_personnelselect', async (req, res) => {
    let data = await PersonnelModel.find()
    res.send({
      data
    })
})
// 人员档案 表   共享
  router.get('/get_personnel', async (req, res) => {
	let name = req.query.name
	let id = req.query.id
	let pagesize=req.query.pagesize
	if(!pagesize){
		pagesize=1
	}
	if(name){
		let datacount = await PersonnelModel.find({suozaibumeng:name}).count()
		let datapage = Math.ceil(await PersonnelModel.find({suozaibumeng:name}).count()/10)
		let data = await PersonnelModel.find({suozaibumeng:name}).limit(10).skip((pagesize-1)*10)
		res.send({ data,datacount,datapage })
	}
  else if(id){
    let data = await PersonnelModel.find({_id:id})
		res.send({ data})
  }
  else{
		let datacount = await PersonnelModel.find().count()
		let datapage = Math.ceil(await PersonnelModel.find().count()/10)
		let data = await PersonnelModel.find().limit(10).skip((pagesize-1)*10)
		res.send({ data,datacount,datapage })
	}
})

// 人员档案表  保存
router.delete("/get_personnel",async(req,res)=>{
    let id = req.query.id
    let data = await PersonnelModel.deleteMany({"_id":id})
    res.send({
      "code":200,
     data
  })
  })
  


// 人员档案 表   添加
router.post("/get_personnel",async(req,res)=>{
    let body = req.body.insetdata
	console.log(body);
    let data = await PersonnelModel.create(body)
    res.send({
      "code":200,
     data
  })
  })
 // 人员档案 表   修改
  router.put("/get_personnel",async(req,res)=>{
    let id = req.query.id
    let body = req.body.insetdata
    let data = await PersonnelModel.updateMany({"_id":id},{$set:body})
    res.send({
      "code":200,
     data
  })
  })


// 人员档案表  保存
router.delete("/get_personnels",async(req,res)=>{
    let id = req.query.id
    let data = await PersonnelModels.deleteMany({"_id":id})
    res.send({
      "code":200,
     data
  })
  })
  
  router.put("/get_personnels",async(req,res)=>{
    let id = req.query.id
    let body = req.body.insetdata
	// console.log(id,body.insetdata);
    let data = await PersonnelModels.updateMany({"_id":id},{$set:body})
    res.send({
      "code":200,
     data
  })
  })
  
  router.post("/get_personnels",async(req,res)=>{
    let body = req.body.insetdata
    let data = await PersonnelModels.create(body)
    res.send({
      "code":200,
     data
  })
  })
  



  router.get('/get_personnels', async (req, res) => {
	let data = await PersonnelModels.find()
	res.send({ data})
})



// 我的资料
router.get('/myprofile', async (req, res) => {
  let id=req.query.id
  let pagesize=req.query.pagesize
  let limitsize=req.query.limitsize
  let name=req.query.name
  let nameselelct=req.query.nameselelct
	if(!pagesize){
		pagesize=1
	}
  if(!limitsize){
		limitsize=10
	}
  if(name){
    let datacount = await mYprofileModels.find({name:name,personnel:"张妍"}).populate("gongxiang").count()
    let datapage = Math.ceil(await mYprofileModels.find({name:name,personnel:"张妍"}).count()/limitsize)
    let data = await mYprofileModels.find({name:name,personnel:"张妍"}).populate("gongxiang").limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
	}else if(id){
      let data = await mYprofileModels.find({_id:id,personnel:"张妍"}).populate("gongxiang")
      res.send({ data})
    }
  else if(nameselelct){
    let datacount = await mYprofileModels.find({nameselect:nameselelct,personnel:"张妍"}).populate("gongxiang").count()
    let datapage = Math.ceil(await mYprofileModels.find({nameselect:nameselelct,personnel:"张妍"}).count()/limitsize)
    let data = await mYprofileModels.find({nameselect:nameselelct,personnel:"张妍"}).populate("gongxiang").limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
  }else{
    let datacount = await mYprofileModels.find({personnel:"张妍"}).count()
    let datapage = Math.ceil(await mYprofileModels.find({personnel:"张妍"}).count()/limitsize)
    let data = await mYprofileModels.find({personnel:"张妍"}).populate("gongxiang").limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
  }

})


// 我的资料添加
router.post("/myprofile",async(req,res)=>{
  let body = req.body.datas
  let data = await mYprofileModels.create(body)
  res.send({
    "code":200,
   data
})
})

// 我的资料修改
router.put("/myprofile",async(req,res)=>{
  let id = req.query.id
  let body = req.body.datas
  let data = await mYprofileModels.updateMany({"_id":id},{$set:body})
  // let data  =[]
  res.send({
    "code":200,
   data
})
})

// 我的资料删除
router.delete("/myprofile",async(req,res)=>{
  let id = req.query.id
  let data = await mYprofileModels.deleteMany({"_id":id})
  res.send({
    "code":200,
   data
})
})

// 资料中心
router.get('/centre', async (req, res) => {
  let id=req.query.id
  let pagesize=req.query.pagesize
  let limitsize=req.query.limitsize
  let name=req.query.name
  let nameselelct=req.query.nameselelct
	if(!pagesize){
		pagesize=1
	}
  if(!limitsize){
		limitsize=10
	}
  if(name){
    let datacount = await mYprofileModels.find({name:name}).count()
    let datapage = Math.ceil(await mYprofileModels.find({name:name}).count()/limitsize)
    let data = await mYprofileModels.find({name:name}).limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
	}else if(id){
      let data = await mYprofileModels.find({_id:id})
      res.send({ data})
    }
  else if(nameselelct){
    let datacount = await mYprofileModels.find({nameselect:nameselelct}).count()
    let datapage = Math.ceil(await mYprofileModels.find({nameselect:nameselelct}).count()/limitsize)
    let data = await mYprofileModels.find({nameselect:nameselelct}).limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
  }else{
    let datacount = await mYprofileModels.find().count()
    let datapage = Math.ceil(await mYprofileModels.find().count()/limitsize)
    let data = await mYprofileModels.find().limit(limitsize).skip((pagesize-1)*limitsize)
    res.send({ data,datacount,datapage })
  }
})


// 我的资料添加
router.post("/centre",async(req,res)=>{
  let body = req.body.datas
  let data = await mYprofileModels.create(body)
  res.send({
    "code":200,
   data
})
})

// 我的资料修改
router.put("/centre",async(req,res)=>{
  let id = req.query.id
  let body = req.body.datas
  let data = await mYprofileModels.updateMany({"_id":id},{$set:body})
  res.send({
    "code":200,
   data
})
})

// 我的资料删除
router.delete("/centre",async(req,res)=>{
  let id = req.query.id
  let data = await mYprofileModels.deleteMany({"_id":id})
  res.send({
    "code":200,
   data
})
})



// 我的资料   树结构   一级查看
router.get("/mYprofileshu_only",async(req,res)=>{
  let name = req.query.name
  let data = await mYprofileshuModels.find({name:name})
  res.send({
    data
  })
})

// 我的资料   树结构   修改
router.put("/mYprofileshuupdate",async(req,res)=>{
  let id = req.query.id
  let body = req.body
  let data = await mYprofileshuModels.updateMany({_id:id},{$set:{name:body.name,sort_sc:body.sort_sc}})
  res.send({
    data
  })
})
//  我的资料   树结构 树形查看
  router.get("/mYprofileshu",async(req,res)=>{

  let ddd = await mYprofileshuModels.aggregate([
    {$lookup:{
      from:"myprofileshu",
      localField:"name_id",
      foreignField:"_id",
      as:"name_id",
      pipeline:[
        {$lookup:{
          from:"myprofileshu",
          localField:"name_id",
          foreignField:"_id",
          as:"name_id",
          pipeline:[
            {$lookup:{
              from:"myprofileshu",
              localField:"name_id",
              foreignField:"_id",
              as:"name_id"}
            },{
              $sort:{sort_sc:1}
            }
          ]
        },
          
        },{
          $sort:{sort_sc:1}
        }
      ]
    }}
    ,{
      $sort:{sort_sc:1}
    }
  ])
  let data = []
  ddd.map(item=>{
    if(item.name_id.length>1){
      data.push(item)
    } 
  })
  // let data = await mYprofileshuModels.find()
  // let data = await mYprofileshuModels.find().populate("name_id").populate("name_id")
  res.send({
    "code":200,
    data
})
})


// 我的资料   树结构
router.post("/mYprofileshuadd",async(req,res)=>{
  let body = req.body
  let data = await mYprofileshuModels.create(body)
  let dataselect = await mYprofileshuModels.find({_id: body.nameselect})
  let list_id = dataselect[0].name_id
  list_id.push(data._id)
  console.log(list_id);
  await mYprofileshuModels.updateMany({_id:body.nameselect},{$set:{name_id:list_id}})
  res.send({
    "code":200,
})
})






// 我的资料   树结构  修改
router.get("/mYprofileshuup",async(req,res)=>{
  let id = req.query.id
  let data = await mYprofileshuModels.updateMany({"_id":id},{$set:{sort_sc:2}})
  res.send({
    "code":200,
    data
})
})


// 我的资料   树结构   删除
router.delete("/mYprofileshus",async(req,res)=>{
  let id = req.query.id
  let data = await mYprofileshuModels.deleteMany({"_id":id})
  res.send({
    "code":200,
    data
})
})

module.exports = router;